package duylinh

import org.apache.commons.fileupload.FileItem
import org.codehaus.groovy.grails.commons.ConfigurationHolder

class UtilsService {

    public static final String PATH_FOR_TEMP_FILES = ConfigurationHolder.config.pathForTempFiles
    public static final String PATH_FOR_FILES = ConfigurationHolder.config.pathForStorage

    public static final String URL_TO_DUYLINH_FS = ConfigurationHolder.config.urlToDuyLinhFS
    public static final String URL_TO_DUYLINH_STORAGE_FS = ConfigurationHolder.config.urlToDuyLinhStoreageFS
    public static final String URL_TO_DUYLINH_STORAGE_VATLIEU_FS = ConfigurationHolder.config.urlToDuyLinhStoreageVatLieuFS
    public static final String URL_TO_DUYLINH_STORAGE_SANPHAM_FS = ConfigurationHolder.config.urlToDuyLinhStoreageSanPhamFS


    def burningImageService

    def saveSanPhamAvatar( sanPham )
    {
        if(   sanPham.avatarUrl
                && sanPham.avatarUrl.startsWith(URL_TO_DUYLINH_FS)  )
        {
            (new File( PATH_FOR_FILES + "/sanpham/sanPham_" + sanPham.id )).mkdir()

            def path = convertUrlToFSPath ( sanPham.avatarUrl )
            def urlToSanPhamFolder =  PATH_FOR_FILES+"/sanpham/sanPham_"+sanPham.id

            println("path "+path)
            println("urlToSanPhamFolder "+urlToSanPhamFolder)


            burningImageService.doWith ( path, urlToSanPhamFolder  )
                    .execute ('thumbnail', {
                it.scaleAccurate(200, 200)
            })
                    .execute ('original', {
                it.scaleAccurate(800, 600)
            })

            sanPham.avatarUrl = URL_TO_DUYLINH_STORAGE_FS + "/sanPham/sanPham_"+sanPham.id + "/thumbnail.jpg"
        }

        return true
    }


    def saveVatLieuAvatar ( vatLieu, avatarUrl )
    {
        println("avatarUrl " + avatarUrl)

        if(   avatarUrl
                && avatarUrl.startsWith(URL_TO_DUYLINH_FS)  )
        {
            (new File( PATH_FOR_FILES + "/vatlieu/vatLieu_" + vatLieu.id )).mkdir()

            def path = convertUrlToFSPath (avatarUrl )
            def urlToVatLieuFolder =  PATH_FOR_FILES+"/vatlieu/vatLieu_"+vatLieu.id

            println("path "+path)
            println("urlToVatLieuFolder "+urlToVatLieuFolder)


            burningImageService.doWith ( path, urlToVatLieuFolder  )
                    .execute ('thumbnail', {
                it.scaleAccurate(200, 200)
            })
                    .execute ('original', {
                it.scaleAccurate(800, 600)
            })

            // vatLieu.avatarUrl = URL_TO_DUYLINH_STORAGE_FS + "/vatLieu_"+vatLieu.id + "/thumbnail.jpg"
        }

        return true
    }


    def convertUrlToFSPath (String url )
    {
        return PATH_FOR_TEMP_FILES + url.split( URL_TO_DUYLINH_FS  )[1]
    }

    /**
     * multipart request -> temp folder on local FS.
     */
    def File saveUploadFS(request, src)
    {
        def multipartFile = request.getFile('file')
        FileItem fileItem = multipartFile.getFileItem()


        File newFile = new File( PATH_FOR_FILES +"/"+ src )
        fileItem.write( newFile )

        return newFile
    }


    /**
     * multipart request -> temp folder on local FS.
     */
    def File tempSaveUploadFS(request)
    {
        def multipartFile = request.getFile('file')
        FileItem fileItem = multipartFile.getFileItem()

        String uuid = this.getRandom(7)

        File newFile = new File(PATH_FOR_TEMP_FILES +"/"+ uuid + ".jpg")
        fileItem.write(newFile)

        return newFile
    }

    /**
     * Kreiert ein String mit zuf�lligen Zahlen mit der gegebener L�nge. Das String ist 128bit gro�.
     * und maximale L�nge betr�gt 36 Zeichen.
     */
    String getRandom(int length)
    {
        UUID uuid = UUID.randomUUID();
        String myRandom = uuid.toString()

        return myRandom.substring(0, length);
    }

}
